package com.hjt.adb.utils;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import org.opencv.core.Point;

import com.hjt.constants.Const;
import com.hjt.dto.AreaInfo;
import com.hjt.utils.Log;
import com.hjt.utils.Util;

public class DeviceUtil {
	public static List<String> devices() {
		List<String> list = new ArrayList<>();
		Process process = ShellUtil.adbCmd(" devices ");
		List<String> result = ShellUtil.readShellOut(process);
		if(result == null) {
			Log.warn("未发现设备信息");
			return list;
		}
		Log.info("设备列表返回信息");
		for(String d: result) {
			if(d == null) {
				continue;
			}
			Log.debug(d);
			if(d.toLowerCase().contains("list") 
					&& d.toLowerCase().contains("devices") 
					&& d.toLowerCase().contains("attached")) {
				continue;
			}
			if(d.toLowerCase().contains("offline")) {
				continue;
			}
			String space = "	";
			if(!d.contains(space)) {
				continue;
			}
			String[] data = d.split(space);
			list.add(data[0].trim());
		}
		Log.info("读取到的设备列表：");
		list.forEach(l -> Log.info(l));
		return list;
	}
	public static BufferedImage screenCap(String device) {
        String baseCmd = String.format("-s %s exec-out screencap -p", device);
        Process cmd = ShellUtil.adbCmd(baseCmd);
        InputStream inputStream = cmd.getInputStream();
        try {
        	return ImageIO.read(inputStream);
		} catch (IOException e) {
			Log.error("截屏【%s】失败", device);
			throw new RuntimeException(e);
		} finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
				}
			}
		}
	}
	public static BufferedImage screenCap(String device, AreaInfo area) {
        String baseCmd = String.format("-s %s exec-out screencap -p", device);
        Process cmd = ShellUtil.adbCmd(baseCmd);
        InputStream inputStream = cmd.getInputStream();
        try {
        	BufferedImage all = ImageIO.read(inputStream);
        	return imgCap(all, area);
		} catch (IOException e) {
			Log.error("截屏【%s】失败", device);
			throw new RuntimeException(e);
		} finally {
			if(inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
				}
			}
		}
	}
	public static BufferedImage imgCap(BufferedImage all, AreaInfo area) {
		return all.getSubimage(area.getX(), area.getY(), area.getW(), area.getH());
	}
	public static Color[][] screenColors(String device, AreaInfo area) {
		BufferedImage buffer = screenCap(device, area);
		Color[][] colors = new Color[buffer.getHeight()][buffer.getWidth()];
		for(int y = 0; y < buffer.getHeight(); y++) {
			for(int x = 0; x < buffer.getWidth(); x++) {
				colors[y][x] = new Color(buffer.getRGB(x, y));
			}
		}
		return colors;
	}
	
	public static void click(String device, Point p) {
		Log.debug("点击位置【x：%s，y：%s】", p.x, p.y);
        ShellUtil.adbCmd(String.format("-s %s shell input tap %s %s",device, p.x, p.y));
        Util.sleep(Const.CLICK_SLEEP);
	}
	
    public static void slideUpDown(String device, Point p, int dis) {
        Process ps = ShellUtil.adbCmd(String.format("-s %s shell input swipe  %s %s %s %s 800", device, p.x, p.y, p.x, p.y + dis));
        while (ps.isAlive()) {
            try {
                Thread.sleep(750);
                ps.destroy();
                Thread.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    
	
	public static void main(String[] args) {
		List<String> devices = DeviceUtil.devices();
		devices.forEach(s -> System.out.println(s));
	}
}
